package com.wenli.graduate2022.wenliGreenShop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenli.graduate2022.wenliGreenShop.entity.Category;
import com.wenli.graduate2022.wenliGreenShop.entity.Product;
import com.wenli.graduate2022.wenliGreenShop.service.servDao.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author HuangHai
 * @date 2021/10/8 21:15
 */
@RestController
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    //查询所有种类
    @GetMapping("category")
    public List<Category> findAll() {
        System.out.println("查询所有方法运行");
        return categoryService.findAll();
    }

    /**
     * 这里的IPage类与Page类都是mybatisPlus给我们自动生成的，只要输入当前页面数与该页面的数据条数，他就会自动帮我们算出需要的数据返回给我们
     * 分页查询种类
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/category/{page}/{size}")
    public IPage<Category> findByPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<Category> categoryPage = new Page<Category>(page, size);
        IPage<Category> categoryIPage = categoryService.findByPage(categoryPage);
        System.out.println("打印页面：" + categoryPage.toString());
        System.out.println("分页查询所有种类：" + categoryIPage.toString());
        return categoryIPage;
    }

    @GetMapping("/category/{id}")
    public Category findById(@PathVariable("id") Integer id) {
        System.out.println("查询某个种类方法运行");
        return categoryService.findById(id);
    }

    @PostMapping("/category")
    public int insert(@RequestBody Category category){
        System.out.println("插入方法运行");
        return categoryService.insert(category);
    }

    @PutMapping("/category")
    public int update(@RequestBody Category category){
        System.out.println("修改方法运行");
        return categoryService.update(category);
    }

    @DeleteMapping("/category/{id}")
    public int delete(@PathVariable("id") Integer id){
        System.out.println("删除方法运行");
        return categoryService.delete(id);
    }

    @GetMapping("/categoryByName/{name}/{page}/{size}")  //这里字符串也可以用restful风格
    public IPage<Category> findByName(@PathVariable("page") Integer page,@PathVariable("size") Integer size,@PathVariable("name") String name){
        System.out.println("模糊查询方法运行");
        Page<Category> productPage = new Page<Category>(page, size);
        return categoryService.findByName(productPage,name);

    }


}
